<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>公司管理管理</title>
    <% include("../layout/css.html"){} %>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="searchSubmit" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="companyTable" lay-filter="companyTable"></table>
            <div class="layui-form layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="companySelSubmit" lay-submit>保存</button>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container layui-hide" >
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData" id="getCheckData">获取选中行数据</button>
    </div>
</script>
<!-- js部分 -->
<% include("../layout/js.html"){} %>
<script type="text/javascript">
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree', 'tableX','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tableX = layui.tableX;
        var laydate = layui.laydate;

        // 渲染表格
        var insTb = tableX.render({
            elem: '#companyTable',
            url: '../company/list',
            page: true,
//            toolbar: true,
            toolbar: '#toolbarDemo',
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{type: 'radio'}
                ,{field: "name", minWidth: "60", title: "公司名称", sort: true, hide: false}
                ,{field: "person", minWidth: "60", title: "联系人", sort: true, hide: false}
                ,{field: "phone", minWidth: "60", title: "联系电话", sort: true, hide: false}
                ,{field: "createTime", minWidth: "60", title: "创建时间", sort: true, hide: true, templet: function (d) {
                    return util.toDateString(d.createTime);
                }}
                ,{field: "createUser", minWidth: "60", title: "创建人", sort: true, hide: true}
                ,{field: "updateTime", minWidth: "60", title: "更新时间", sort: true, hide: true, templet: function (d) {
                    return util.toDateString(d.createTime);
                }}
                ,{field: "updateUser", minWidth: "60", title: "更新人", sort: true, hide: true}
            ]]
            ,where:{}
            ,done: function(res, page, count){
                //可以自行添加判断的条件是否选中
                //这句才是真正选中，通过设置关键字LAY_CHECKED为true选中，这里只对第一行选中
//                res.data[0]["LAY_CHECKED"]='true';
                //下面三句是通过更改css来实现选中的效果
                $.each(res.data, function(k,v){
                    if(v.id=="${company.id}"){
                        $('tr[data-index=' + k + '] input[type="radio"]').parent().find("i").click();
//                        $('tr[data-index=' + k + '] input[type="radio"]').prop('checked', true);
//                        $('tr[data-index=' + k + '] input[type="radio"]').next().addClass('layui-form-checked');
//                         form.render('radio');
                    }
                })

            }
        });

        //监听行单击事件（单击事件为：rowDouble）
        table.on('row(companyTable)', function(obj){
            // console.log(obj.tr.attr("data-index"))
            //选中行样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            //选中radio样式
            obj.tr.find('i[class="layui-anim layui-icon"]').trigger("click");
            // $('tr[data-index=' + obj.tr.attr("data-index") + '] input[type="radio"]').parent().find("i").click();
           /* obj.tr.find('input[type="radio"]').prop('checked', true);
            form.render('radio');*/
            /*var data = obj.data;

            layer.alert(JSON.stringify(data), {
                title: '当前行数据：'
            });

            //标注选中样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');*/
//            $(obj.tr[0].outerHTML).attr("data-index")
        });

        // 搜索
        form.on('submit(searchSubmit)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
        });
        // 搜索
        form.on('submit(companySelSubmit)', function (data) {
            $("#getCheckData").click();
        });

        // 工具条点击事件
        table.on('toolbar(companyTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            switch(obj.event){
                case 'getCheckData':
                    getCheckData(obj,checkStatus);
                    break;
            };
        });
        function getCheckData(obj,checkStatus){
            var checkData = checkStatus.data;  //获取选中行数据
            //layer.alert(JSON.stringify(data));
            if(checkData.length>0){
                top.companySel(checkData);
                admin.closeThisDialog();
            } else {
                layer.alert("请选择公司!");
            }
        }

    });
</script>
</body>
</html>
